#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll inf = 1e17;
const int N = 2e5+10;

ll mx[N<<2],tag[N<<2];

void pu(int k){
    mx[k]=max(mx[k<<1],mx[k<<1|1]);
}

void pd(int k){
    if (tag[k]){
        mx[k<<1]+=tag[k];
        mx[k<<1|1]+=tag[k];
        tag[k<<1]+=tag[k];
        tag[k<<1|1]+=tag[k];
        tag[k]=0;
    }
}

void bd(int k,int l,int r){
    mx[k]=-inf;
    tag[k]=0;
    if (l==r){
        return;
    }
    int mid=l+r>>1;
    bd(k<<1,l,mid);
    bd(k<<1|1,mid+1,r);
}

void upd(int k,int l,int r,int ql,int qr,ll v){
    if (ql<=l && r<=qr){
        mx[k]+=v;
        tag[k]+=v;
        return;
    }
    if (l>qr || r<ql){
        return;
    }
    int mid=l+r>>1;
    pd(k);
    upd(k<<1,l,mid,ql,qr,v);
    upd(k<<1|1,mid+1,r,ql,qr,v);
    pu(k);
}

void updp(int k,int l,int r,int p,ll v){
    if (l==r){
        mx[k]=max(mx[k],v);
        tag[k]=0;
        return;
    }
    int mid=l+r>>1;
    pd(k);
    if (p<=mid) updp(k<<1,l,mid,p,v);
    else updp(k<<1|1,mid+1,r,p,v);
    pu(k);
}

ll qy(int k,int l,int r,int ql,int qr){
    if (ql<=l && r<=qr){
        return mx[k];
    }
    if (l>qr || r<ql){
        return -inf;
    }
    int mid=l+r>>1;
    pd(k);
    return max(qy(k<<1,l,mid,ql,qr),qy(k<<1|1,mid+1,r,ql,qr));
}

const int M = 1e6+6;

int n,a[N],id[N],cl[M],cr[M],rk[N];

bool cmp(int x,int y){
    return a[x]<a[y];
}

void solve(){
    cin>>n;
    for (int i=3; i<=n+2; i++){
        cin>>a[i];
        cl[a[i]]=cr[a[i]]=-1;
        id[i]=i;
    }
    rk[1]=1,rk[2]=2;
    sort(id+3,id+3+n,cmp);
    for (int i=3; i<=n+2; i++){
        rk[id[i]]=i;
    }
    for (int i=3; i<=n+2; i++){
       // cout<<id[i]<<" ";
        if (cl[a[id[i]]]==-1) cl[a[id[i]]]=i;
        cr[a[id[i]]]=i;
    }//cout<<"\n";
    bd(1,1,n+2); // j: 1~n+2 i: 3~n+2
    updp(1,1,n+2,1,0);
    for (int i=3; i<=n+2; i++){
        int a1=(a[i]==a[i-1])*a[i];
        ll mx1=qy(1,1,n+2,1,n+2);
        ll mx2=qy(1,1,n+2,cl[a[i]],cr[a[i]])+a[i];
      //  mx1=max(mx1,0ll),mx2=max(mx2,0ll)+a[i];
        upd(1,1,n+2,1,n+2,a1);
        updp(1,1,n+2,rk[i-1],max(mx1,mx2));
       // cout<<cl[a[i]]<<","<<cr[a[i]]<<","<<rk[i-1]<<"\n";
       // for (int j=1; j<=n+2; j++) cout<<qy(1,1,n+2,j,j)<<" ";
       // cout<<"\n";
    }
    cout<<qy(1,1,n+2,1,n+2)<<"\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    cin>>t;
    while (t--){
        solve();
    }
    return 0;
}

/*
Things to check:
    - int/long long overflow?
    - mod, +mod, %mod?
    - array bounds?
    - Places where you think can't be wrong.
    - Functions should return if not "void".
    - what is the memory limit? the mod? the name etc.
Things to think of:
    - Be brave to guess and be optimistic.
    - Start from a trivial case.
    - What is the expected time to finish this problem?
Do not be afraid of a problem, try.
Do not devote all your time into a problem, look at other problems.
*/